<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/27
 * Time: 17:22
 */
namespace app\common\helper;

use app\emotion\config\emotion_config;

class emotion_helper
{
    // 表情变文字码
    public static function code_to_emoji($str)
    {
        foreach ( config('emotion.emoji_map') as $k=>$v ) {
            $str = str_replace($v, $k, $str);
        }

        return $str;
    }

    // 表情变文字码
    public static function emoji_to_code($str)
    {
        foreach ( emotion_config::$emoji_map as $k=>$v ) {
            $str = str_replace($k, $v, $str);
        }

        return $str;
    }

    /**
     * 表情替换成文本
     * @param string $str
     * @return string
     */
    public static function image_to_str($str)
    {
        $pattern = '/(<img title="(.+?)".+?>)/';
        $flag = preg_match_all($pattern, $str, $matches);
        //var_dump($flag, $matches);
        if ($flag && isset($matches[2]) && $matches[2]) {
            foreach ($matches[2] as $k=>$v) {
                $str = str_replace($matches[1][$k], '['.$v.']', $str);
            }
        }

        return $str;
    }
}